import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import server from '@/utils/server';
import { ElMessage } from 'element-plus';
import {useUsererStore} from '@/stores/user'

export const useBlogStore = defineStore('blogStore', () => {
  // 查询个人博客
  const myBlogList = ref()
  const searchMyblog = () => {
    server.get('/api/blog/searchMyblog', {
      page: 1,
      size: 5
    }).then(res => {
      myBlogList.value = res.data?.list
    })
  }
  const clearMyblog = () => {
    myBlogList.value = []
  }

  const isSignin = ref(false)
  // 签到操作
  const signin = () => {
  const userStore = useUsererStore()
    if(!userStore.userInfo?.realname){
      ElMessage.warning('请先登录')
      return
    }
    server.get(`/api/user/signin`).then(res => {
      if (res.errno === 1) {
        ElMessage.warning(res.message)
        return
      }
      isSignin.value = true
      ElMessage.success(res.message)
    })
  }
  // 查询签到
  const seachSAignin = () => {
    server.get(`/api/user/isSigin`).then(res => {
      console.log(res, 'resres')
      if(res.errno === 1) {
        isSignin.value = true
        return
      }
      isSignin.value = false
    })
  }
  const resetSingin = () => {
    isSignin.value = false
  }
  return {
    myBlogList,
    searchMyblog,
    clearMyblog,
    isSignin,
    signin,
    resetSingin,
    seachSAignin
  }
})
